package me.zhengjie.modules.transfer.factory.service.Impl;

import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.transfer.factory.service.TransOutDealService;
import me.zhengjie.modules.transfer.factory.vo.Ic91Info;
import me.zhengjie.modules.transfer.factory.vo.TransferPersonInfo;
import me.zhengjie.tool.*;
import me.zhengjie.vo.Constant;
import me.zhengjie.vo.StandardVO;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 转出账户处理类实现类
 */
@Service
@RequiredArgsConstructor
public class TransOutDealServiceImpl implements TransOutDealService {

    private static final long serialVersionUID = 1234567L;




    /**
     * 处理缴费（退费、补差）
     *
     * @return
     */
@Override
    public Map dealAccount(List<Map> paymentList) {

    List<Map> paymentListCal = new ArrayList<>();

    List<Map> paymentListXXB = new ArrayList<>();


    NPETool.isEmptyThrowException(paymentList, "缴费明细");


        //云南 单独处理 建账前缴费 只有金额的缴费数据 转出省外需要拆分，

        List<Map> list = paymentList.stream().filter(p -> BigDecimal.ZERO.compareTo(NumberUtil.nullDispose(p.get("aae180"))) == 0
                && BigDecimal.ZERO.compareTo(NumberUtil.nullDispose(p.get("aaa045"))) == 0
                && BigDecimal.ZERO.compareTo(NumberUtil.nullDispose(p.get("aaa042"))) == 0
                && BigDecimal.ZERO.compareTo(NumberUtil.nullDispose(p.get("aae023"))) < 0).collect(Collectors.toList());
        if (ValidateUtil.isNotEmpty(list)) {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("对应费款所属期【");
            list.forEach(l -> {
                stringBuilder.append(l.get("aae003") + "】，");
            });
            stringBuilder.append("未拆分，请拆分后再办理转出！");
            throw new IllegalArgumentException(stringBuilder.toString());
        }


        //统模式以后得数据全部分组汇总   相同期号 应缴类型和地方应缴类型一致的 合并处理  主要是因为统模式以后的数据 存在 正常应缴、基数补收、比例补收 个人一条 单位一条
        List<Map> tms = paymentList.stream().filter(ac08 -> Integer.parseInt(ac08.get("aae003").toString()) >= 202307).collect(Collectors.toList());
        paymentList.removeAll(tms);


        // 汇总金额字段
        Map<String, Map> summary = tms.stream()
                .collect(Collectors.toMap(
                        record -> record.get("aae003").toString() + "-" + record.get("aaa115").toString() + "-" + record.get("aae793").toString(), // 根据字段汇总
                        record -> {
                            Map<String, Object> newRecord = new HashMap<>();
                            newRecord.putAll(record);
                            return newRecord;
                        },
                        (existing, replacement) -> {
                            existing.put("aab121", new BigDecimal(existing.get("aab121").toString()).add(new BigDecimal(replacement.get("aab121").toString())));
                            existing.put("aae180", new BigDecimal(existing.get("aae180").toString()).add(new BigDecimal(replacement.get("aae180").toString())));
                            existing.put("aae795", new BigDecimal(existing.get("aae795").toString()).add(new BigDecimal(replacement.get("aae795").toString())));
                            existing.put("aae871", new BigDecimal(existing.get("aae871").toString()).add(new BigDecimal(replacement.get("aae871").toString())));
                            existing.put("aae080", new BigDecimal(existing.get("aae080").toString()).add(new BigDecimal(replacement.get("aae080").toString())));
                            existing.put("aae082", new BigDecimal(existing.get("aae082").toString()).add(new BigDecimal(replacement.get("aae082").toString())));
                            existing.put("aae083", new BigDecimal(existing.get("aae083").toString()).add(new BigDecimal(replacement.get("aae083").toString())));
                            existing.put("aae202", new BigDecimal(existing.get("aae202").toString()).add(new BigDecimal(replacement.get("aae202").toString())));
                            existing.put("aaa041", new BigDecimal(existing.get("aaa041").toString()).add(new BigDecimal(replacement.get("aaa041").toString())));
                            existing.put("aaa042", new BigDecimal(existing.get("aaa042").toString()).add(new BigDecimal(replacement.get("aaa042").toString())));
                            existing.put("aaa043", new BigDecimal(existing.get("aaa043").toString()).add(new BigDecimal(replacement.get("aaa043").toString())));
                            existing.put("aaa045", new BigDecimal(existing.get("aaa045").toString()).add(new BigDecimal(replacement.get("aaa045").toString())));
                            return existing;
                        }
                ));

        paymentList.addAll(new ArrayList<>(summary.values()));
        System.out.println(paymentList);


        //云南特殊处理存在比例调整的
        List<Map> ac08_35 = paymentList.stream().filter(ac08 -> ac08.get("aaa115").equals(Constant.AAA115_35_JFBLDZBJ) || ac08.get("aaa115").equals(Constant.AAA115_21_JFBLDZTS)).collect(Collectors.toList());
        paymentList.removeAll(ac08_35);
        //缴费基数调整退收
        List<Map> ac08_2200 = paymentList.stream().filter(ac08 -> ac08.get("aae793").equals(Constant.AAE793_2200_JFJSDZTS)).collect(Collectors.toList());//缴费基数调整退收
        paymentList.removeAll(ac08_2200);


        //统模式导致的灵活就业人员 个人一条单位一条 明细问题处理
        List<Map> ac08_1005 = paymentList.stream().filter(ac08 -> Constant.AAE793_1005_GTJF.equals(ac08.get("aae793")) && "0".equals(ac08.get("aae202").toString())).collect(Collectors.toList());
        paymentList.removeAll(ac08_1005);


        //建账前的明细不作为转移明细
        List<Map> ac08_9024 = paymentList.stream().filter(ac08 -> ac08.get("aae793").equals(Constant.AAE793_9024_JZQJFYS)).collect(Collectors.toList());
        paymentList.removeAll(ac08_9024);


        //202307统模式上线导致的ac08单位一条，个人一条记录合并 (正常应缴)
        List<Map> ac08_tms = paymentList.stream().filter(ac08 -> Integer.parseInt(ac08.get("aae003").toString()) >= 202307 &&
                BigDecimalUtil.getBigDecimal(ac08.get("aae180").toString()).compareTo(BigDecimal.ZERO) == 0
                && BigDecimalUtil.getBigDecimal(ac08.get("aae080").toString()).compareTo(BigDecimal.ZERO) > 0
                && BigDecimalUtil.getBigDecimal(ac08.get("aae082").toString()).compareTo(BigDecimal.ZERO) == 0
                && BigDecimalUtil.getBigDecimal(ac08.get("aae202").toString()).compareTo(BigDecimal.ZERO) == 0
        ).collect(Collectors.toList());
        paymentList.removeAll(ac08_tms);

        List<String> aae793_3401 = new ArrayList<>();
        List<String> aaz648s_tf = new ArrayList<>();//退费记录合并过的aaz648 合并过的就不在退费


        StandardVO standardVO1 = new StandardVO("aae003", "Integer", "asc");
        StandardVO standardVO2 = new StandardVO("aae202", "Integer", "desc");
        paymentList = ListUtil.sorts(paymentList, standardVO1, standardVO2);
        for (Map payment : paymentList) {

            for (Map map : ac08_35) {//云南 存在比例调整的，比例调整的比例还原到正常缴费那条  （原纪录默认取aae202=1的则为原纪录，因为存在中断补收，补录，aae793不一定都是1000）
                if (map.get("aae003").toString().equals(payment.get("aae003").toString()) && "1".equals(payment.get("aae202").toString())) {
                    payment.put("aaa042", new BigDecimal(payment.get("aaa042").toString()).add(new BigDecimal(map.get("aaa042").toString())));

                }

            }

            for (Map map : ac08_2200) {//云南 缴费基数调整退收，调整原记录基数 （原纪录默认取aae202=1的则为原纪录，因为存在中断补收，补录，aae793不一定都是1000）

                //考虑到 存在 退费的基数比正常应该基数大的情况，正常情况 退费和正常应缴的台账冲销  正常应缴小于退费的，和基数调整的冲销
                if (map.get("aae003").toString().equals(payment.get("aae003").toString()) && "1".equals(payment.get("aae202").toString()) && (!aaz648s_tf.contains(map.get("aaz648").toString())) && (new BigDecimal(payment.get("aae180").toString())).compareTo((new BigDecimal(map.get("aae180").toString()).abs())) >= 0) {
                    payment.put("aac040", new BigDecimal(payment.get("aac040").toString()).add(new BigDecimal(map.get("aac040").toString())));
                    payment.put("aab121", new BigDecimal(payment.get("aab121").toString()).add(new BigDecimal(map.get("aab121").toString())));
                    payment.put("aae180", new BigDecimal(payment.get("aae180").toString()).add(new BigDecimal(map.get("aae180").toString())));
                    payment.put("aae795", new BigDecimal(payment.get("aae795").toString()).add(new BigDecimal(map.get("aae795").toString())));
                    payment.put("aae871", new BigDecimal(payment.get("aae871").toString()).add(new BigDecimal(map.get("aae871").toString())));
                    payment.put("aae080", new BigDecimal(payment.get("aae080").toString()).add(new BigDecimal(map.get("aae080").toString())));
                    payment.put("aae082", new BigDecimal(payment.get("aae082").toString()).add(new BigDecimal(map.get("aae082").toString())));
                    payment.put("aae083", new BigDecimal(payment.get("aae083").toString()).add(new BigDecimal(map.get("aae083").toString())));
                    aaz648s_tf.add(map.get("aaz648").toString());
                    //考虑到 会存在多条记录调整补收的，只合并一次，合并过的期号将不再合并(原理：基数调整比退费大的那条基数和退费的做合并处理)
                } else if (map.get("aae003").toString().equals(payment.get("aae003").toString()) && Constant.AAE793_3401_CEBS.equals(payment.get("aae793").toString()) && (!aaz648s_tf.contains(map.get("aaz648").toString())) && (new BigDecimal(payment.get("aae180").toString())).compareTo((new BigDecimal(map.get("aae180").toString()).abs())) >= 0) {
                    System.out.println("aae003:" + map.get("aae003") + "  aae793:" + payment.get("aae793").toString() + "  3401：" + payment.get("aae180").toString() + "  2200:" + map.get("aae180").toString());
                    payment.put("aac040", new BigDecimal(payment.get("aac040").toString()).add(new BigDecimal(map.get("aac040").toString())));
                    payment.put("aab121", new BigDecimal(payment.get("aab121").toString()).add(new BigDecimal(map.get("aab121").toString())));
                    payment.put("aae180", new BigDecimal(payment.get("aae180").toString()).add(new BigDecimal(map.get("aae180").toString())));
                    payment.put("aae795", new BigDecimal(payment.get("aae795").toString()).add(new BigDecimal(map.get("aae795").toString())));
                    payment.put("aae871", new BigDecimal(payment.get("aae871").toString()).add(new BigDecimal(map.get("aae871").toString())));
                    payment.put("aae080", new BigDecimal(payment.get("aae080").toString()).add(new BigDecimal(map.get("aae080").toString())));
                    payment.put("aae082", new BigDecimal(payment.get("aae082").toString()).add(new BigDecimal(map.get("aae082").toString())));
                    payment.put("aae083", new BigDecimal(payment.get("aae083").toString()).add(new BigDecimal(map.get("aae083").toString())));

                    aaz648s_tf.add(map.get("aaz648").toString());
                }


            }


            for (Map map : ac08_tms) {//云南 统模式导致的 单位一条个人一条数据处理

                if (map.get("aae003").toString().equals(payment.get("aae003").toString()) && "1".equals(payment.get("aae202").toString())) {
                    payment.put("aac040", new BigDecimal(payment.get("aac040").toString()));
                    payment.put("aab121", new BigDecimal(map.get("aab121").toString()));
                    payment.put("aae180", new BigDecimal(payment.get("aae180").toString()));
                    payment.put("aae795", new BigDecimal(payment.get("aae795").toString()));
                    payment.put("aae871", new BigDecimal(payment.get("aae871").toString()));
                    payment.put("aae080", new BigDecimal(payment.get("aae080").toString()).add(new BigDecimal(map.get("aae080").toString())));
                    payment.put("aae082", new BigDecimal(payment.get("aae082").toString()).add(new BigDecimal(map.get("aae082").toString())));
                    payment.put("aae083", new BigDecimal(payment.get("aae083").toString()).add(new BigDecimal(map.get("aae083").toString())));

                    payment.put("aaa042", new BigDecimal(payment.get("aaa042").toString()).add(new BigDecimal(map.get("aaa042").toString())));
                    payment.put("aaa043", new BigDecimal(payment.get("aaa043").toString()).add(new BigDecimal(map.get("aaa043").toString())));
                    payment.put("aaa041", new BigDecimal(payment.get("aaa041").toString()).add(new BigDecimal(map.get("aaa041").toString())));
                    payment.put("aaa045", new BigDecimal(payment.get("aaa045").toString()).add(new BigDecimal(map.get("aaa045").toString())));

                }
            }


        }


        //处理云南 因缓缴 导致的同一个月数据有两条，一条单位有金额，一条有个人金额的情况
        //根据期号，应缴类型，基数，单位比例，个人划账户比例分组
        Map<String, List<Map>> paymentGroup = paymentList.stream().collect(Collectors.groupingBy(m -> m.get("aae003").toString()
                + m.get("aaa115") + m.get("aae180") + m.get("aaa041") + m.get("aaaa045") + m.get("aae792")));

        List<Map> removeList = new ArrayList<>();

        Map deleteMap;
        for (List<Map> list1 : paymentGroup.values()) {
            if (list1.size() == 2) {
                deleteMap = returnEqualUnitPayment(list1);
                if (!ValidateUtil.isEmpty(deleteMap)) {
                    removeList.add(deleteMap);
                }
            }
        }
        paymentList.removeAll(removeList);

        //预缴处理，把费款期当做到账日期
        paymentList.forEach(ac08 -> {
            if (ValidateUtil.isEmpty(ac08.get("aae793"))) {
                throw new IllegalArgumentException("费款所属期【" + ac08.get("aae793") + "】本地应缴类型为空！");
            }
            if (ac08.get("aae003").toString().substring(0, 4).compareTo(ac08.get("aab191").toString().substring(0, 4)) > 0
                    && ac08.get("aae003").toString().compareTo(ac08.get("aab191").toString().substring(0, 6)) > 0) {
                ac08.put("aab191", ac08.get("aae003") + "01");
            }
        });

        //正常状态的实缴信息  aae792 in (01,04)
        List<Map> noramlList = paymentList.stream().filter(ac08 ->
                (Constant.AAE792_01_ZC.equals(ac08.get("aae792")) || Constant.AAE792_04_QKQQ.equals(ac08.get("aae792")))

        ).collect(Collectors.toList());

        //退费信息
        List<Map> refundList = paymentList.stream().filter(ac08 -> Constant.AAE792_05_TF.equals(ac08.get("aae792"))).collect(Collectors.toList());

        //云南退费数据原数据迁移的aae792 =06  退费原数据还得筛选一次
        List<Map> refundOldList = new ArrayList<>();
        if (ValidateUtil.isNotEmpty(refundList)) {
            for (Map m : refundList) {
                for (Map ac08 : paymentList) {
                    if (ac08.get("aaz686").toString().equals(m.get("aaz686").toString()) && !Constant.AAE792_05_TF.equals(ac08.get("aae792").toString()) && ac08.get("aac040").toString().equals(m.get("aac040").toString().replace("-", ""))) {
                        refundOldList.add(ac08);
                        break;

                    }
                }

            }
        }

        //添加 参与记息
        if (ValidateUtil.isNotEmpty(refundOldList)) {
            noramlList.addAll(refundOldList);
        }

        if (ValidateUtil.isNotEmpty(refundList)) {
            //取退费对应收入缴费明细的到账日期
            boolean flag;

            for (Map ac08_tf : refundList) {
                flag = false;
                for (Map ac08_normal : noramlList) {

                    if (ac08_normal.get("aaz686").toString().equals(ac08_tf.get("aaz686").toString())
                            //加上应缴类型对比
                            && !(ac08_tf.get("aaa115").toString().equals(Constant.AAA115_21_JFBLDZTS) || ac08_tf.get("aaa115").toString().equals(Constant.AAA115_22_JFJSDZTS) || ac08_tf.get("aaa115").toString().equals(Constant.AAA115_34_JFJSDZBJ) || ac08_tf.get("aaa115").toString().equals(Constant.AAA115_35_JFBLDZBJ))) {
                        flag = true;
                        ac08_tf.put("aab191", ac08_normal.get("aab191"));
                        break;
                    }
                }
                if (!flag) {
                    throw new IllegalArgumentException("费款所属期【" + ac08_tf.get("aae003") + "】存在退费记录，但不存在退费原缴费记录！");

                }
            }

            paymentListCal.addAll(refundList);
        }


        //处理基数补差 比例补差
        List<Map> bcList = noramlList.stream().filter(
                ac08 -> (Constant.AAA115_22_JFJSDZTS.equals(ac08.get("aaa115"))
                        || Constant.AAA115_21_JFBLDZTS.equals(ac08.get("aaa115"))
                        || (Constant.AAA115_34_JFJSDZBJ.equals(ac08.get("aaa115")) && Double.parseDouble(ac08.get("aaa042").toString()) >= 0)//因补差导致的单位缴费比例-0.01的数据，先过滤, 在后面方法统一处理
                        || Constant.AAA115_35_JFBLDZBJ.equals(ac08.get("aaa115")))
        ).collect(Collectors.toList());
        //处理补差(本地数据，转移记录aaa115 不为21，22，34，35)
        if (ValidateUtil.isNotEmpty(bcList)) {
            //去掉补差的数据 防止重复添加
            noramlList.removeAll(bcList);
            //只取有月数的缴费
            List<Map> filterList = noramlList.stream().filter(ac08 -> Integer.valueOf(ac08.get("aae202").toString()) > 0).collect(Collectors.toList());
            //取基数补退对应的 正常缴费的到账日期  ，
            //取比例补退对应的 正常缴费的到账日期  ，
            //基数补退比例如果为空 取原记录的比例
            //比例补退基数如果为空 取原记录的基数
            boolean flag;
            for (Map ac08_bc : bcList) {
                flag = false;
                for (Map ac08_normal : filterList) {
                    if (ac08_normal.get("aae003").toString().equals(ac08_bc.get("aae003").toString())) {
                        flag = true;

                        if (Constant.AAA115_22_JFJSDZTS.equals(ac08_bc.get("aaa115"))) {

                            ac08_bc.put("aab191", ac08_normal.get("aab191"));
                            if (ValidateUtil.isEmpty(ac08_bc.get("aaa045")) || BigDecimal.ZERO.compareTo(BigDecimalUtil.getBigDecimal(ac08_bc.get("aaa045").toString())) == 0) {
                                ac08_bc.put("aaa045", ac08_normal.get("aaa045"));
                                ac08_bc.put("aaa043", ac08_normal.get("aaa043"));
                            }

                        } else if (Constant.AAA115_21_JFBLDZTS.equals(ac08_bc.get("aaa115"))) {

                            ac08_bc.put("aab191", ac08_normal.get("aab191"));
                            if (ValidateUtil.isEmpty(ac08_bc.get("aae180")) || BigDecimal.ZERO.compareTo(BigDecimalUtil.getBigDecimal(ac08_bc.get("aae180").toString())) == 0) {
                                ac08_bc.put("aae180", ac08_normal.get("aae180"));
                            }

                        } else if (Constant.AAA115_34_JFJSDZBJ.equals(ac08_bc.get("aaa115"))) {

                            if (ValidateUtil.isEmpty(ac08_bc.get("aaa045")) || BigDecimal.ZERO.compareTo(BigDecimalUtil.getBigDecimal(ac08_bc.get("aaa045").toString())) == 0) {
                                ac08_bc.put("aaa045", ac08_normal.get("aaa045"));
                                ac08_bc.put("aaa043", ac08_normal.get("aaa043"));
                            }

                        } else if (Constant.AAA115_35_JFBLDZBJ.equals(ac08_bc.get("aaa115"))) {

                            if (ValidateUtil.isEmpty(ac08_bc.get("aae180")) || BigDecimal.ZERO.compareTo(BigDecimalUtil.getBigDecimal(ac08_bc.get("aae180").toString())) == 0) {
                                ac08_bc.put("aae180", ac08_normal.get("aae180"));
                            }

                        }

                        break;
                    }
                }
                if (!flag) {
                    throw new IllegalArgumentException("费款所属期【" + ac08_bc.get("aae003") + "】存在基数或比例补差缴费，但不存在原缴费记录！");
                }
            }

            //由于 云南存在同一个月同时有正常缴费 和 基数、比例补收的数据，做退费只生成一条ac08，所以基数、比例补收 存在退费的 ，把基数、比例补收的数据和原数据作合并处理
            //退费数据和原记录一对一的不用调用这一段代码
            if (ValidateUtil.isNotEmpty(refundList)) {
                bcList.forEach(ac08_bc -> {
                    refundList.forEach(ac08_tf -> {

                        if (ac08_tf.get("aaz686").toString().equals(ac08_bc.get("aaz686").toString())) {
                            //由于前面已经处理了退费的到账时间，已经和正常的保持一致，这里直接取退费的到账时间
                            ac08_bc.put("aab191", ac08_tf.get("aab191"));
                        }

                    });
                });
            }

            paymentListCal.addAll(bcList);
        }


        List<Map> paymentListTf = new ArrayList<>();
        noramlList.forEach(normalPayment -> {
            MyMap nor = new MyMap(normalPayment);

            // 单位缴费比例-0.01问题
            if ((Constant.AAE793_1061_ZR.equals(nor.getAsString("aae793")) || Constant.AAE793_1062_JGZR.equals(nor.getAsString("aae793")) || Constant.AAE793_3401_CEBS.equals(nor.getAsString("aae793"))) && Double.parseDouble(nor.getAsString("aaa042")) < 0) {
                paymentListTf.add(normalPayment);
                String aae003 = nor.getAsString("aae003");
                for (Map normal : noramlList) {

                    if (aae003.equals(normal.get("aae003").toString()) && Integer.valueOf(normal.get("aae202").toString()).intValue() > 0) {
                        normal.put("aae080", Double.parseDouble(normal.get("aae080").toString()) + Double.parseDouble(nor.get("aae080").toString()));
                        normal.put("aaa042", Double.parseDouble(normal.get("aaa042").toString()) + Double.parseDouble(nor.get("aaa042").toString()));

                        break;
                    }
                }

            }

            // 老系统转移包含退费问题处理
            if ((Constant.AAE793_1061_ZR.equals(nor.getAsString("aae793")) || Constant.AAE793_1062_JGZR.equals(nor.getAsString("aae793"))) && Double.parseDouble(nor.getAsString("aae180")) < 0) {
                paymentListTf.add(normalPayment);
                String aae003 = nor.getAsString("aae003");
                for (Map normal : noramlList) {

                    if (aae003.equals(normal.get("aae003").toString()) && Integer.valueOf(normal.get("aae202").toString()).intValue() > 0) {
                        normal.put("aab121", Double.parseDouble(normal.get("aab121").toString()) + Double.parseDouble(nor.get("aab121").toString()));
                        normal.put("aae180", Double.parseDouble(normal.get("aae180").toString()) + Double.parseDouble(nor.get("aae180").toString()));
                        normal.put("aae795", Double.parseDouble(normal.get("aae795").toString()) + Double.parseDouble(nor.get("aae795").toString()));
                        normal.put("aae871", Double.parseDouble(normal.get("aae871").toString()) + Double.parseDouble(nor.get("aae871").toString()));
                        normal.put("aae080", Double.parseDouble(normal.get("aae080").toString()) + Double.parseDouble(nor.get("aae080").toString()));
                        normal.put("aae082", Double.parseDouble(normal.get("aae082").toString()) + Double.parseDouble(nor.get("aae082").toString()));
                        normal.put("aae083", Double.parseDouble(normal.get("aae083").toString()) + Double.parseDouble(nor.get("aae083").toString()));
                        break;
                    }
                }

            }

        });


        noramlList.forEach(normalPayment -> {
            MyMap nor = new MyMap(normalPayment);
            //云南转移缴费记录 补差数据 缴费比例为空 取对应转移缴费原记录的比例
            if (Constant.AAE793_1061_ZR.equals(nor.getAsString("aae793")) || Constant.AAE793_1062_JGZR.equals(nor.getAsString("aae793")) && 0 == nor.getAsInteger("aae202").intValue()) {
                //默认基数大于0 ，月数等于0  为转移缴费补差记录
                if (!ValidateUtil.isEmpty(nor.getAsBigDecimal("aae180")) && BigDecimal.ZERO.compareTo(nor.getAsBigDecimal("aae180")) < 0
                        && (ValidateUtil.isEmpty(nor.getAsBigDecimal("aaa045")) || BigDecimal.ZERO.compareTo(nor.getAsBigDecimal("aaa045")) == 0
                        || ValidateUtil.isEmpty(nor.getAsBigDecimal("aaa042")) || BigDecimal.ZERO.compareTo(nor.getAsBigDecimal("aaa042")) == 0)) {
                    String aae003 = nor.getAsString("aae003");
                    for (Map normal : noramlList) {

                        if (aae003.equals(normal.get("aae003").toString()) && Integer.valueOf(normal.get("aae202").toString()).intValue() > 0) {
                            normalPayment.put("aaa042", normal.get("aaa042"));
                            normalPayment.put("aaa045", normal.get("aaa045"));
                            break;
                        }
                    }
                }
            }
        });

        paymentListCal.addAll(noramlList);
        paymentListCal.removeAll(paymentListTf);

        if (ValidateUtil.isNotEmpty(paymentListCal)) {
            paymentListXXB.addAll(paymentListCal);

            if (ValidateUtil.isNotEmpty(refundList)) {
                Iterator<Map> iterator = paymentListXXB.iterator();
                Map ac08;
                while (iterator.hasNext()) {
                    ac08 = iterator.next();
                    for (Map refund : refundList) {
                        if (ac08.get("aaz686").toString().equals(refund.get("aaz686").toString())) {
                            iterator.remove();
                            break;
                        }
                    }
                }
            }

            if (ValidateUtil.isNotEmpty(paymentListXXB)) {
                //云南 单独处理 建账前缴费 只有金额的缴费数据 转出省外需要拆分，省内不需要拆分，过滤掉
                paymentListXXB = paymentListXXB.stream().filter(p -> !(BigDecimal.ZERO.compareTo(NumberUtil.nullDispose(p.get("aae180"))) == 0
                        //   && BigDecimal.ZERO.compareTo(NumberUtil.nullDispose(p.get("aae202"))) == 0
                        && BigDecimal.ZERO.compareTo(NumberUtil.nullDispose(p.get("aaa045"))) == 0
                        && BigDecimal.ZERO.compareTo(NumberUtil.nullDispose(p.get("aaa042"))) == 0
                        && BigDecimal.ZERO.compareTo(NumberUtil.nullDispose(p.get("aae023"))) < 0)).collect(Collectors.toList());
            }
            NPETool.isEmptyThrowException(paymentListXXB, "缴费明细");
        }

    HashMap resultMap = new HashMap<>();
    resultMap.put("paymentListXXB",paymentListXXB);
    resultMap.put("paymentListCal",paymentListCal);

    return resultMap;
    }

    /**
     * 返回 同一个月中 只有单位缴费的那一条
     *
     * @param list
     * @return
     */
    private Map returnEqualUnitPayment(List<Map> list) {

        if (ValidateUtil.isEmpty(list) || list.size() != 2) {
            return null;
        }
        Map m1 = list.get(0);
        Map m2 = list.get(1);
        if (Constant.AAE792_01_ZC.equals(m1.get("aae792").toString()) || Constant.AAE792_04_QKQQ.equals(m1.get("aae792").toString())) {
            if ((ValidateUtil.isEmpty(m1.get("aae023")) || BigDecimal.ZERO.compareTo(BigDecimalUtil.getBigDecimal(m1.get("aae023").toString())) == 0)
                    && (ValidateUtil.isEmpty(m2.get("aae080")) || BigDecimal.ZERO.compareTo(BigDecimalUtil.getBigDecimal(m1.get("aae080").toString())) == 0)) {

                return m2;
            } else if ((ValidateUtil.isEmpty(m1.get("aae080")) || BigDecimal.ZERO.compareTo(BigDecimalUtil.getBigDecimal(m1.get("aae080").toString())) == 0)
                    && (ValidateUtil.isEmpty(m2.get("aae023")) || BigDecimal.ZERO.compareTo(BigDecimalUtil.getBigDecimal(m1.get("aae023").toString())) == 0)) {
                return m1;
            }
        }
        return null;
    }

    /**
     * 缴费记录比例为空的 设置比例
     *
     * @param ac08Map
     */
    @Override
    public void setNullProportion(MyMap ac08Map) {

        int aae003 = ac08Map.getAsInteger("aae003");
        if (ac08Map.get("aac066").equals(Constant.AAC066_102_YGRSFCBRY)) {
            ac08Map.put("aaa042", "0.12");
        } else if (ac08Map.get("aac066").equals(Constant.AAC066_101_QYZG)) {
            if (aae003 <= 201604 && (ValidateUtil.isEmpty(ac08Map.getAsBigDecimal("aaa042")) || BigDecimal.ZERO.compareTo(ac08Map.getAsBigDecimal("aaa042")) == 0)) {
                ac08Map.put("aaa042", "0.2");
            } else if (201605 <= aae003 && aae003 <= 201904 && (ValidateUtil.isEmpty(ac08Map.getAsBigDecimal("aaa042")) || BigDecimal.ZERO.compareTo(ac08Map.getAsBigDecimal("aaa042")) == 0)) {
                ac08Map.put("aaa042", "0.19");
            } else if (ValidateUtil.isEmpty(ac08Map.getAsBigDecimal("aaa042")) || BigDecimal.ZERO.compareTo(ac08Map.getAsBigDecimal("aaa042")) == 0) {
                ac08Map.put("aaa042", "0.16");
            }
        } else if (ac08Map.get("aac066").equals(Constant.AAC066_301_GWY) || ac08Map.get("aac066").equals(Constant.AAC066_302_SYBZRY)) {
            if (aae003 <= 201904 && (ValidateUtil.isEmpty(ac08Map.getAsBigDecimal("aaa042")) || BigDecimal.ZERO.compareTo(ac08Map.getAsBigDecimal("aaa042")) == 0)) {
                ac08Map.put("aaa042", "0.2");
            } else if (ValidateUtil.isEmpty(ac08Map.getAsBigDecimal("aaa042"))) {
                ac08Map.put("aaa042", "0.16");
            }
        } else {
            throw new IllegalArgumentException(aae003 + "缴费明细参保身份错误！");
        }
        if (aae003 >= 200601 && (ValidateUtil.isEmpty(ac08Map.getAsBigDecimal("aaa045")) || BigDecimal.ZERO.compareTo(ac08Map.getAsBigDecimal("aaa045")) == 0)) {
            ac08Map.put("aaa045", "0.08");
        }
    }


    /**
     * 备注 "包括建账前个人缴费" 特殊处理(云南省内转移 采集账户不拆分，特殊处理 备注包括建账前个人缴费)
     *
     * @param informationVOS
     */
    @Override
    public void dealWithAccountBeforeAac032(List<Ic91Info> informationVOS, TransferPersonInfo transferPersonInfo,List<Map>paymentList ) {

        if (ValidateUtil.isEmpty(informationVOS)) {
            return;
        }

        List<Map> list = paymentList.stream().filter(p -> BigDecimal.ZERO.compareTo(NumberUtil.nullDispose(p.get("aae180"))) == 0
                && BigDecimal.ZERO.compareTo(NumberUtil.nullDispose(p.get("aae202"))) == 0
                && BigDecimal.ZERO.compareTo(NumberUtil.nullDispose(p.get("aaa045"))) == 0
                && BigDecimal.ZERO.compareTo(NumberUtil.nullDispose(p.get("aaa042"))) == 0
                && BigDecimal.ZERO.compareTo(NumberUtil.nullDispose(p.get("aae023"))) < 0).collect(Collectors.toList());
        if (ValidateUtil.isNotEmpty(list)) {

            // 循环处理数据
            for (Ic91Info transferInformationVO : informationVOS) {

                //处理建账前数据
                if (!ValidateUtil.isEmpty(transferPersonInfo.getAae206())) {
                    if (transferInformationVO.getAae001().intValue() == transferPersonInfo.getAae206().intValue() / 100
                            && !ValidateUtil.isEmpty(transferInformationVO.getAae382()) && transferInformationVO.getAae382().compareTo(BigDecimal.ZERO) > 0) {

                        setAae013(transferInformationVO, "包括建账前个人缴费");
                        break;
                    }

                    //还需要考虑 建账当年做业务年度切换的情况
                    if (transferInformationVO.getAae001().intValue() - 1 == transferPersonInfo.getAae206().intValue() / 100
                            && !ValidateUtil.isEmpty(transferInformationVO.getAae382()) && transferInformationVO.getAae382().compareTo(BigDecimal.ZERO) > 0) {

                        setAae013(transferInformationVO, "包括建账前个人缴费");
                        break;
                    }
                }
            }

        }

    }


    private void setAae013(Ic91Info transferInformationVO, String aae013) {
        if (ValidateUtil.isNotEmpty(transferInformationVO.getAae013())) {
            transferInformationVO.setAae013(transferInformationVO.getAae013() + "," + aae013);
        } else {
            transferInformationVO.setAae013(aae013);
        }
    }



@Override
    public List<MyMap> getNewAc51s(List<Map> ac51s, List<MyMap> ac08s, BigDecimal aae206) {

        //查询账户
        List<MyMap> newAc51s = new ArrayList<>();
        if (ValidateUtil.isNotEmpty(ac51s)) {

            Map<String, List<Map>> groupAc51 = ac51s.stream().collect(Collectors.groupingBy(info -> info.get("aae001").toString()));


            List<Map> ac51sCal = new ArrayList<>();

            Map ac51Map = null;
            MyMap ac51MyMap = null;
            List<Map> ac51GroupList;
            for (String aae001 : groupAc51.keySet()) {
                ac51GroupList = groupAc51.get(aae001);
                ac51Map = new MyMap();
                BigDecimal aae849 = BigDecimal.ZERO; //截至上年末个人账户单位缴费部分划转累计本息 N NUMBER -->
                BigDecimal aae259 = BigDecimal.ZERO; //截至上年末个人账户单位缴费部分划转本息在本年产生的利息 N NUMBER -->
                BigDecimal aae262 = BigDecimal.ZERO; //本年单位缴费部分划入个人账户本金 N NUMBER -->
                BigDecimal aae263 = BigDecimal.ZERO; //本年单位缴费部分划入个人账户本金所产生的利息 N NUMBER -->
                BigDecimal aae850 = BigDecimal.ZERO; //截至本年末个人账户单位缴费部分划转累计本息 N NUMBER -->
                BigDecimal aae851 = BigDecimal.ZERO; //截至上年末个人账户个人缴费部分划转累计本息 N NUMBER -->
                BigDecimal aae260 = BigDecimal.ZERO; //截至上年末个人账户个人缴费部分划转本息在本年产生的利息 N NUMBER -->
                BigDecimal aae264 = BigDecimal.ZERO; //本年个人缴费部分划入个人账户本金 N NUMBER -->
                BigDecimal aae265 = BigDecimal.ZERO; //本年个人缴费部分划入个人账户本金所产生的利息 N NUMBER -->
                BigDecimal aae852 = BigDecimal.ZERO; //截至本年末个人账户个人缴费部分划转累计本息 N NUMBER -->
                BigDecimal aae853 = BigDecimal.ZERO; //截至上年末个人账户其他部分划转累计本息 N NUMBER -->
                BigDecimal aae854 = BigDecimal.ZERO; //截至上年末个人账户其他部分划转本息在本年产生的利息 N NUMBER -->
                BigDecimal aae855 = BigDecimal.ZERO; //本年其他部分划入个人账户本金 N NUMBER -->
                BigDecimal aae856 = BigDecimal.ZERO; //本年其他部分划入个人账户本金所产生的利息 N NUMBER -->
                BigDecimal aae857 = BigDecimal.ZERO; //截至本年末个人账户其他部分划转累计本息 N NUMBER -->
                for (Map map : ac51GroupList) {
                    ac51MyMap = new MyMap(map);
                    aae849 = aae849.add(ValidateUtil.isEmpty(ac51MyMap.getAsBigDecimal("aae849")) ? BigDecimal.ZERO : ac51MyMap.getAsBigDecimal("aae849"));
                    aae259 = aae259.add(ValidateUtil.isEmpty(ac51MyMap.getAsBigDecimal("aae259")) ? BigDecimal.ZERO : ac51MyMap.getAsBigDecimal("aae259"));
                    aae262 = aae262.add(ValidateUtil.isEmpty(ac51MyMap.getAsBigDecimal("aae262")) ? BigDecimal.ZERO : ac51MyMap.getAsBigDecimal("aae262"));
                    aae263 = aae263.add(ValidateUtil.isEmpty(ac51MyMap.getAsBigDecimal("aae263")) ? BigDecimal.ZERO : ac51MyMap.getAsBigDecimal("aae263"));
                    aae850 = aae850.add(ValidateUtil.isEmpty(ac51MyMap.getAsBigDecimal("aae850")) ? BigDecimal.ZERO : ac51MyMap.getAsBigDecimal("aae850"));
                    aae851 = aae851.add(ValidateUtil.isEmpty(ac51MyMap.getAsBigDecimal("aae851")) ? BigDecimal.ZERO : ac51MyMap.getAsBigDecimal("aae851"));
                    aae260 = aae260.add(ValidateUtil.isEmpty(ac51MyMap.getAsBigDecimal("aae260")) ? BigDecimal.ZERO : ac51MyMap.getAsBigDecimal("aae260"));
                    aae264 = aae264.add(ValidateUtil.isEmpty(ac51MyMap.getAsBigDecimal("aae264")) ? BigDecimal.ZERO : ac51MyMap.getAsBigDecimal("aae264"));
                    aae265 = aae265.add(ValidateUtil.isEmpty(ac51MyMap.getAsBigDecimal("aae265")) ? BigDecimal.ZERO : ac51MyMap.getAsBigDecimal("aae265"));
                    aae852 = aae852.add(ValidateUtil.isEmpty(ac51MyMap.getAsBigDecimal("aae852")) ? BigDecimal.ZERO : ac51MyMap.getAsBigDecimal("aae852"));
                    aae853 = aae853.add(ValidateUtil.isEmpty(ac51MyMap.getAsBigDecimal("aae853")) ? BigDecimal.ZERO : ac51MyMap.getAsBigDecimal("aae853"));
                    aae854 = aae854.add(ValidateUtil.isEmpty(ac51MyMap.getAsBigDecimal("aae854")) ? BigDecimal.ZERO : ac51MyMap.getAsBigDecimal("aae854"));
                    aae855 = aae855.add(ValidateUtil.isEmpty(ac51MyMap.getAsBigDecimal("aae855")) ? BigDecimal.ZERO : ac51MyMap.getAsBigDecimal("aae855"));
                    aae856 = aae856.add(ValidateUtil.isEmpty(ac51MyMap.getAsBigDecimal("aae856")) ? BigDecimal.ZERO : ac51MyMap.getAsBigDecimal("aae856"));
                    aae857 = aae857.add(ValidateUtil.isEmpty(ac51MyMap.getAsBigDecimal("aae857")) ? BigDecimal.ZERO : ac51MyMap.getAsBigDecimal("aae857"));
                }

                ac51Map.put("aae001", aae001);
                ac51Map.put("aae849", aae849);
                ac51Map.put("aae259", aae259);
                ac51Map.put("aae262", aae262);
                ac51Map.put("aae263", aae263);
                ac51Map.put("aae850", aae850);
                ac51Map.put("aae851", aae851);
                ac51Map.put("aae260", aae260);
                ac51Map.put("aae264", aae264);
                ac51Map.put("aae265", aae265);
                ac51Map.put("aae852", aae852);
                ac51Map.put("aae853", aae853);
                ac51Map.put("aae854", aae854);
                ac51Map.put("aae855", aae855);
                ac51Map.put("aae856", aae856);
                ac51Map.put("aae857", aae857);

                ac51sCal.add(ac51Map);
            }
            //查询历年台账
            if (ValidateUtil.isNotEmpty(ac51sCal)) {
                //按bae500降序
                Collections.sort(ac51sCal, new Comparator<Map>() {

                    public int compare(Map o1, Map o2) {
                        Integer i1 = Integer.valueOf(o1.get("aae001").toString());
                        Integer i2 = Integer.valueOf(o2.get("aae001").toString());
                        if (i1.intValue() > i2.intValue()) {
                            return 1;
                        }
                        return -1;
                    }
                });
                newAc51s = convertAc51(ac51sCal);
            }
        }

        return newAc51s;
    }

    /**
     * 将中台查询出的ac51 汇总成符合信息表的台帐
     *
     * @param ac51s
     * @return
     */

    public List<MyMap> convertAc51(List<Map> ac51s) {
        List<MyMap> ac51List = new ArrayList<>();


        //BigDecimal aae849 = BigDecimal.ZERO;//截至上年末个人账户单位缴费部分划转累计本息
        //BigDecimal aae850 = BigDecimal.ZERO;//截至本年末个人账户单位缴费部分划转累计本息
        //BigDecimal aae851 = BigDecimal.ZERO;//截至上年末个人账户个人缴费部分划转累计本息
        //BigDecimal aae852 = BigDecimal.ZERO;//截至本年末个人账户个人缴费部分划转累计本息
        //BigDecimal aae262 = BigDecimal.ZERO;//本年单位缴费部分划入个人账户本金
        //BigDecimal aae263 = BigDecimal.ZERO;//本年单位缴费部分划入个人账户本金所产生的利息
        //BigDecimal aae264 = BigDecimal.ZERO;//本年个人缴费部分划入个人账户本金
        //BigDecimal aae265 = BigDecimal.ZERO;//本年个人缴费部分划入个人账户本金所产生的利息
        //BigDecimal aae259 = BigDecimal.ZERO;//截至上年末个人账户单位缴费部分划转本息在本年产生的利息
        //BigDecimal aae260 = BigDecimal.ZERO;//截至上年末个人账户个人缴费部分划转本息在本年产生的利息
        //BigDecimal aae873 = BigDecimal.ZERO;//本年本金总和
        //BigDecimal aae874 = BigDecimal.ZERO;//本年利息总和
        //BigDecimal aae875 = BigDecimal.ZERO;//本年补缴历年本金
        //BigDecimal aae876 = BigDecimal.ZERO;//本年补缴历年利息


        MyMap ac51Map;
        MyMap resultMap;
        for (Map m : ac51s) {
            ac51Map = new MyMap(m);
            if (BigDecimal.ZERO.compareTo(ac51Map.getAsBigDecimal("aae849")) == 0
                    && BigDecimal.ZERO.compareTo(ac51Map.getAsBigDecimal("aae259")) == 0
                    && BigDecimal.ZERO.compareTo(ac51Map.getAsBigDecimal("aae262")) == 0
                    && BigDecimal.ZERO.compareTo(ac51Map.getAsBigDecimal("aae263")) == 0
                    && BigDecimal.ZERO.compareTo(ac51Map.getAsBigDecimal("aae850")) == 0
                    && BigDecimal.ZERO.compareTo(ac51Map.getAsBigDecimal("aae851")) == 0
                    && BigDecimal.ZERO.compareTo(ac51Map.getAsBigDecimal("aae260")) == 0
                    && BigDecimal.ZERO.compareTo(ac51Map.getAsBigDecimal("aae264")) == 0
                    && BigDecimal.ZERO.compareTo(ac51Map.getAsBigDecimal("aae265")) == 0
                    && BigDecimal.ZERO.compareTo(ac51Map.getAsBigDecimal("aae852")) == 0
                    && BigDecimal.ZERO.compareTo(ac51Map.getAsBigDecimal("aae853")) == 0
                    && BigDecimal.ZERO.compareTo(ac51Map.getAsBigDecimal("aae854")) == 0
                    && BigDecimal.ZERO.compareTo(ac51Map.getAsBigDecimal("aae855")) == 0
                    && BigDecimal.ZERO.compareTo(ac51Map.getAsBigDecimal("aae856")) == 0
                    && BigDecimal.ZERO.compareTo(ac51Map.getAsBigDecimal("aae857")) == 0) {
                continue;

            }
            resultMap = new MyMap();
            resultMap.put("aae001", ac51Map.getAsBigDecimal("aae001"));//当年记账金额
            resultMap.put("aae381", ac51Map.getAsBigDecimal("aae262")
                    .add(ac51Map.getAsBigDecimal("aae264"))
                    .add(ac51Map.getAsBigDecimal("aae855")));//当年记账金额
            resultMap.put("aae264", ac51Map.getAsBigDecimal("aae264"));//当年记账金额个人部分
            resultMap.put("aae269", ac51Map.getAsBigDecimal("aae263")
                    .add(ac51Map.getAsBigDecimal("aae265"))
                    .add(ac51Map.getAsBigDecimal("aae856")));//当年记账利息
            resultMap.put("aae265", ac51Map.getAsBigDecimal("aae265"));//当年记账利息个人部分
            resultMap.put("aae382", ac51Map.getAsBigDecimal("aae850")
                    .add(ac51Map.getAsBigDecimal("aae852"))
                    .add(ac51Map.getAsBigDecimal("aae857")));//至本年末账户累计储存额
            resultMap.put("aae273", ac51Map.getAsBigDecimal("aae852"));//至本年末账户累计储存额中的个人缴费部分
            ac51List.add(resultMap);
        }
        return ac51List;
    }
}


